package com.paic.icore.test.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.paic.icore.test.demo.dao.UserMapper;
import com.paic.icore.test.demo.service.CountryService;
import com.paic.icore.test.demo.util.Test;
import com.paic.icore.test.demo.util.TestUti;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.paic.icore.test.demo.dao.UserRepository;
import com.paic.icore.test.demo.entity.Country;
import com.paic.icore.test.demo.entity.User;
import com.paic.icore.test.demo.service.UserService;

import java.util.Optional;

/**
 * 描述:
 *
 * @author HUXINGJIE666
 * @date 2017年12月25日下午2:24:55
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CountryService countryService;

    @Autowired
    private Test testUti;

    /**
     * @param name
     * @return
     */
    @Override
    public User findUserByName(String name) {
        User u2 = userMapper.selectByPrimaryKey(1L);
        log.info("u2 :{}", JSON.toJSONString(u2));
//        try {
//            countryService.deleteUser(2L);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        delete();
        return u2;
    }

    public void save2(User user) {
        user.setAddress("save2");
        userMapper.updateByPrimaryKey(user);
        throw new RuntimeException();
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete() {
        userMapper.deleteByPrimaryKey(2L);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public User findUserById(Long id) {
        Optional<User> byId = userRepository.findById(id);
        if (byId.isPresent()) {
            return byId.get();
        }
        return null;
    }

    // 读取到未提交数据
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    @Override
    public User findUserById2(Long id) {
        return userRepository.findById(id).get();
    }

    //    @Transactional(propagation = Propagation.REQUIRES_NEW)
    private User udpateByUser(User u1) {
        u1.setAddress("北京");
        userMapper.updateByPrimaryKeySelective(u1);
        return u1;
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public User updateUser(User user) {
        User oldUser = userRepository.findById(user.getId()).get();
        BeanUtils.copyProperties(user, oldUser, new String[]{"address", "teamId", "birth", "age"});
        userRepository.save(oldUser);
        updateCountry("MYS");
        userRepository.flush();
        User newuser = userRepository.findById(user.getId()).get();
        return newuser;
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void updateCountry(String code) {
    }
}
